/**
 *  @author Ion Morozan
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.dddforandroid.design;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.dddforandroid.database.R;
import com.dddforandroid.database.local.DeleteNote;
import com.dddforandroid.database.server.NotePad.Notes;
import com.dddforandroid.database.server.SyncServerDatabase;

public class TasksArrayAdapter extends ArrayAdapter<String> {
	private final Context context;
	private final String[] values;
	private final ArrayList<Notes> notes;
	public static SyncServerDatabase nosql;
	public static ImageButton deleteButton;

	public TasksArrayAdapter(Context context, String[] values,
			ArrayList<Notes> notes, SyncServerDatabase nosql) {
		super(context, R.layout.list_view_row, values);
		this.context = context;
		this.values = values;
		this.notes = notes;
		TasksArrayAdapter.nosql = nosql;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowView = inflater.inflate(R.layout.list_view_row, parent, false);

		TextView textView = (TextView) rowView.findViewById(R.id.label);
		deleteButton = (ImageButton) rowView.findViewById(R.id.delete);
		ImageButton editButton = (ImageButton) rowView
				.findViewById(R.id.modify);

		textView.setText(values[position]);
		final int itemClicked = position;

		deleteButton.setImageResource(R.drawable.delete);
		deleteButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				 
				deleteButton.setOnClickListener(null);
				Toast.makeText(context, "Wait...", Toast.LENGTH_LONG)
						.show();

				v.setTag(itemClicked);
				String name, note, id;
				name = notes.get(itemClicked).getTitle();
				note = notes.get(itemClicked).getNote();
				id = notes.get(itemClicked).getId();

				try {
					new DeleteNote(name, note, id, context, nosql,
							CurrentTasksActivity.authClient).execute().get();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ExecutionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});

		editButton.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {


				String name, note, id;
				long date;
				name = notes.get(itemClicked).getTitle();
				note = notes.get(itemClicked).getNote();
				date = notes.get(itemClicked).getCreatedDate();
				id = notes.get(itemClicked).getId();

				Intent intentTasks = new Intent(context,
						ModifyTaskActivity.class);
				intentTasks.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

				intentTasks.putExtra("name", name);
				intentTasks.putExtra("note", note);
				intentTasks.putExtra("date", date);
				intentTasks.putExtra("id", id);

				context.startActivity(intentTasks);
			}
		});

		return rowView;
	}
}